package com.tf_cut.backend.person.service.impl;


import com.tf_cut.backend.common.pojo.PageInfo;
import com.tf_cut.backend.common.pojo.Result;
import com.tf_cut.backend.common.service.CommonServiceImpl;
import com.tf_cut.backend.mapper.PersonBillMapper;
import com.tf_cut.backend.person.pojo.PersonBill;
import com.tf_cut.backend.person.repository.PersonBillRepository;
import com.tf_cut.backend.person.service.PersonBillService;
import com.tf_cut.backend.person.service.PersonConsigneeService;
import com.tf_cut.backend.person.vo.PersonBillAndConsigneeVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import java.util.List;

@Service
@Transactional
@Slf4j
public class PersonBillServiceImpl extends CommonServiceImpl<PersonBillAndConsigneeVo, PersonBill, String> implements PersonBillService {

    @PersistenceContext
    private EntityManager em;
    @Autowired
    private PersonBillRepository personBillRepository;
    @Autowired
    private PersonConsigneeService personConsigneeService;
    @Autowired
    private PersonBillMapper personBillMapper;

    @Override
    public Result<PersonBillAndConsigneeVo> insertBillAndConsignee(PersonBillAndConsigneeVo avo) {
        //保存收件人
        Result<PersonBillAndConsigneeVo> result = personConsigneeService.save(avo);
        //保存收件人id
        avo.setRecivePersonName(result.getData().getRecivePersonId());
        // 新建bill
        result = this.save(avo);
        result.setData(avo);
        return result;
    }

    @Override
    public PageInfo<PersonBillAndConsigneeVo> queryPersonBillList(PersonBillAndConsigneeVo personBillAndConsigneeVo) {
        com.github.pagehelper.PageHelper.startPage(personBillAndConsigneeVo.getPage(), personBillAndConsigneeVo.getRows());
        List<PersonBillAndConsigneeVo> list = personBillMapper.queryPersonBillList(personBillAndConsigneeVo);
        com.github.pagehelper.PageInfo<PersonBillAndConsigneeVo> pageInfo = new com.github.pagehelper.PageInfo<>(list);

        PageInfo<PersonBillAndConsigneeVo> returnPageInfo = new PageInfo<>();
        returnPageInfo.setPage(pageInfo.getPageNum());
        returnPageInfo.setPageSize(pageInfo.getPageSize());
        returnPageInfo.setRows(pageInfo.getList());
        returnPageInfo.setRecords((int) pageInfo.getTotal());
        returnPageInfo.setTotal(pageInfo.getPages());
        return returnPageInfo;
    }
}
